/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：NavCfgService.java
 * History:
 *         2012-6-26: Initially created, Chrise.
 */
package com.xteam.ecsystem.service.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.CommendType;
import com.xteam.ecsystem.base.NavPosition;
import com.xteam.ecsystem.base.NavType;
import com.xteam.ecsystem.base.NormalPage;
import com.xteam.ecsystem.base.NormalState;
import com.xteam.ecsystem.base.SystemContent;
import com.xteam.ecsystem.dao.IArticlesSortDao;
import com.xteam.ecsystem.dao.IGoodsGroupDao;
import com.xteam.ecsystem.dao.INavCfgDao;
import com.xteam.ecsystem.entities.SysNavConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 导航配置服务层。
 * @author Chrise
 */
@Service
public class NavCfgService {
	
	@Autowired
	private INavCfgDao ncDao;
	
	@Autowired
	private IGoodsGroupDao ggDao;
	
	@Autowired
	private IArticlesSortDao agDao;
	
	/**
	 * 获取导航配置。
	 * @author Chrise 2012-6-26
	 * @param sncType
	 * @param sncName
	 * @param sncPage
	 * @param sncPosition
	 * @param sncState
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageCenterStation<SysNavConfig> getNavConfigs(int sncType, String sncName, 
		int sncPage, int sncPosition, int sncState, int pageIndex, int pageSize) {
		if (sncType == NavType.ALL.value()) sncType = INavCfgDao.INVALID_TYPE;
		if (sncName == null || "".equals(sncName)) sncName = INavCfgDao.INVALID_NAME;
		if (sncPage == NormalPage.ALL.value()) sncPage = INavCfgDao.INVALID_PAGE;
		if (sncPosition == NavPosition.ALL.value()) sncPosition = INavCfgDao.INVALID_POSITION;
		if (sncState == NormalState.ALL.value()) sncState = INavCfgDao.INVALID_STATE;
		
		return this.ncDao.getConfigs(sncType, sncName, sncPage, sncPosition, sncState, pageIndex * pageSize, pageSize);
	}
	
	/**
	 * 切换状态。
	 * @author Chrise 2012-6-26
	 * @param sncId
	 * @param sncTo
	 */
	public void switchState(String sncId, boolean sncTo) {
		if (sncTo) {
			this.ncDao.updateState(sncId, NormalState.EFFECTIVE.value());
		} else {
			this.ncDao.updateState(sncId, NormalState.INVALID.value());
		}
	}
	
	/**
	 * 删除导航配置。
	 * @author Chrise 2012-6-26
	 * @param ids
	 */
	public void delNavConfigs(List<String> ids) {
		this.ncDao.deleteConfigs(ids);
	}
	
	/**
	 * 保存导航配置。
	 * @author Chrise 2012-6-26
	 * @param snc
	 * @return
	 */
	public boolean saveNavConfig(SysNavConfig snc) {
		if (snc.getSncState() == null) {
			snc.setSncState(NormalState.EFFECTIVE.value());
		}
		
		if (snc.getSncId() == null || "".equals(snc.getSncId())) {
			if (this.ncDao.isExist(snc.getSncName())) return false;
			
			this.ncDao.insertConfig(snc);
		} else {
			if (this.ncDao.isExist(snc.getSncId(), snc.getSncName())) return false;
			
			this.ncDao.updateConfig(snc);
		}
		
		return true;
	}
	
	/**
	 * 获取导航配置。
	 * @author Chrise 2012-6-26
	 * @param id
	 * @return
	 */
	public SysNavConfig getNavConfig(String id) {
		return this.ncDao.getConfig(id);
	}
	
	/**
	 * 获取导航内容。
	 * @author Chrise 2012-6-26
	 * @param type
	 * @return
	 */
	public List<SystemContent> getNavContent(int type) {
		List<Map<String, String>> scl = null;
		
		NavType nt = NavType.valueOf(type);
		switch (nt) {
			case COMMEND:
				scl = CommendType.getCommendType();
				break;
			case GOODS:
				scl = this.ggDao.getSystemContent(NormalState.EFFECTIVE.value(), 
					SystemContent.FN_VALUE, SystemContent.FN_TEXT, SystemContent.FN_PARENT);
				break;
			case ARTICLE:
				scl = this.agDao.getSystemContent(NormalState.EFFECTIVE.value(), 
					SystemContent.FN_VALUE, SystemContent.FN_TEXT, SystemContent.FN_PARENT);
				break;
		}
		
		List<SystemContent> list = new ArrayList<SystemContent>();
		
		if (scl != null) {
			for (Map<String, String> sc : scl) {
				list.add(new SystemContent(sc.get(SystemContent.FN_VALUE), 
					sc.get(SystemContent.FN_TEXT), sc.get(SystemContent.FN_PARENT)));
			}
		}
		
		return list;
	}
	
	/**
	 * 获取导航内容。
	 * @author Chrise 2012-6-29
	 * @param type
	 * @param name
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageCenterStation<SystemContent> getNavContents(int type, String name, int pageIndex, int pageSize) {
		NavType nt = NavType.valueOf(type);
		switch (nt) {
			case GOODS:
				return this.ggDao.getSystemContents(name, NormalState.EFFECTIVE.value(), pageIndex * pageSize, pageSize);
			case ARTICLE:
				return this.agDao.getSystemContents(name, NormalState.EFFECTIVE.value(), pageIndex * pageSize, pageSize);
		}
		
		return new PageCenterStation<SystemContent>(0, null);
	}
}
